package com.base.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.base.user.dao.GZDAO;
import com.base.user.entity.GZ;
import com.base.user.service.GZService;

import cn.mysteel.util.ObjectUtils;

@Service("gzService")
public class GZServiceImpl implements GZService 
{
	@Autowired
	private GZDAO gzDao;
	
	@Override
	public void addGZ(GZ gz)
	{
		Assert.notNull (gz);
		
		long memberId = gz.getMemberId ();
		long friend_id = gz.getFriend_id ();
		
		Map<String, Object> params = new HashMap<String,Object>();
		
		params.put ("memberId" ,memberId);
		params.put ("friend_id" ,friend_id);
		
		GZ g = gzDao.getByMap (params);
		
		if(ObjectUtils.isNull (g))
		{
			gz.setCreateTime (System.currentTimeMillis ());
			gzDao.save (gz);
		}
	}

	@Override
	public void deleteGZ(long memberId, long friend_id)
	{
		
		Map<String, Object> params = new HashMap<String,Object>();
		
		params.put ("memberId" ,memberId);
		params.put ("friend_id" ,friend_id);
		
		GZ g = gzDao.getByMap (params);
		
		if(ObjectUtils.notNull (g))
		{
			gzDao.delete (g.getId ());
		}
	}

	@Override
	public List<GZ> getAllGZByMemberId(long memberId)
	{
		
		return gzDao.getAllGZByMemberId (memberId);
	}

	@Override
	public boolean isGZ(long memberId, long friend_id)
	{
		Map<String, Object> params = new HashMap<String,Object>();
		
		params.put ("memberId" ,memberId);
		params.put ("friend_id" ,friend_id);
		
		GZ g = gzDao.getByMap (params);
		
		if(ObjectUtils.notNull (g))
		{
			
			return true;
		}
		
		return false;
	}

	@Override
	public boolean isEachOtherGZ(long memberId, long friend_id)
	{
		
		Map<String, Object> params1 = new HashMap<String,Object>();
		
		params1.put ("memberId" ,memberId);
		params1.put ("friend_id" ,friend_id);
		
		GZ g1 = gzDao.getByMap (params1);
		
		Map<String, Object> params2 = new HashMap<String,Object>();
		
		params2.put ("memberId" ,friend_id);
		params2.put ("friend_id" ,memberId);
		
		GZ g2 = gzDao.getByMap (params2);
		
		if(ObjectUtils.notNull (g1)&&ObjectUtils.notNull (g2))
		{
			
			return true;
		}
		
		return false;
	}
}
